package com.yishusmt.wechatapi.wechatdev.serviceImpl;

import com.yishusmt.wechatapi.wechatdev.dto.base.Result;
import com.yishusmt.wechatapi.wechatdev.dto.request.OrderReq;
import com.yishusmt.wechatapi.wechatdev.dto.request.WeChatOrderReq;
import com.yishusmt.wechatapi.wechatdev.dto.response.OrderRes;
import com.yishusmt.wechatapi.wechatdev.entity.UserEntity;
import com.yishusmt.wechatapi.wechatdev.entity.WechatOrderEntity;
import com.yishusmt.wechatapi.wechatdev.mapper.UserEntityMapper;
import com.yishusmt.wechatapi.wechatdev.mapper.WechatOrderEntityMapper;
import com.yishusmt.wechatapi.wechatdev.mapper.WechatOrderInfoMapper;
import com.yishusmt.wechatapi.wechatdev.service.WeChatOrderService;
import com.yishusmt.wechatapi.wechatdev.utils.AliyunSmsUtil;
import com.yishusmt.wechatapi.wechatdev.utils.DateUtil;
import com.yishusmt.wechatapi.wechatdev.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

/**
 * @author zhangyi
 * @date 2018/9/7 17:12
 */
@Service
public class WeChatOrderServiceImpl implements WeChatOrderService {

    @Autowired
    private WechatOrderEntityMapper wechatOrderEntityMapper;

    @Autowired
    private WechatOrderInfoMapper wechatOrderInfoMapper;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private UserEntityMapper userEntityMapper;

    /**
     * 下单
     *
     * @param weChatOrderReq
     * @return
     */
    @Override
    public Result addOrder(WeChatOrderReq weChatOrderReq) {
        String code = redisUtil.get(weChatOrderReq.getOpenid());
        boolean present = Optional.ofNullable(code).isPresent();
        if(present && code.equals(weChatOrderReq.getCode())){
            //验证码校验
            WechatOrderEntity wechatOrderEntity = new WechatOrderEntity();
            wechatOrderEntity.setOpenid(weChatOrderReq.getOpenid());
            wechatOrderEntity.setCounts(weChatOrderReq.getCounts());
            wechatOrderEntity.setContact(weChatOrderReq.getContact());
            wechatOrderEntity.setContactName(weChatOrderReq.getContactName());
            wechatOrderEntity.setProjectName(weChatOrderReq.getProjectName());
            wechatOrderEntity.setDeliveryPeriod(weChatOrderReq.getDeliveryPeriod());
            wechatOrderEntity.setOrderNo(String.valueOf(System.nanoTime()));
            wechatOrderEntity.setCreateTime(DateUtil.getLocalDate());
            wechatOrderEntity.setUpdateTime(DateUtil.getLocalDate());


            //添加专员信息(需求是固定的项目专员 15:37 2018-09-13)
            wechatOrderEntity.setUserId(new Long(67));
            int i = wechatOrderEntityMapper.insertSelective(wechatOrderEntity);
            new Thread(() -> {
                if(0 == i?false:true){
                    //发送通知短信 此处写死
                    AliyunSmsUtil.sendNotice("18758033862",weChatOrderReq.getContactName());
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }).start();
            return 0 == i?new Result(false):new Result(true);
        }else{
            return new Result("验证码输入错误");
        }
    }

    /**
     * 分类获取订单
     *
     * @param status
     * @return
     */
    @Override
    public Result getOrderList(Long memberId,List<Integer> status) {
        List<OrderRes> orderRes = null;
        if(memberId != null) {
            OrderReq orderReq = new OrderReq(memberId, status);
            orderRes  = wechatOrderInfoMapper.selectOrderListByMemberId(orderReq);
        }
        return new Result(orderRes);
    }

    /**
     * 获取订单详细信息
     *
     * @param id
     * @return
     */
    @Override
    public Result getOrderInfo(Long id) {
        WechatOrderEntity wechatOrderEntity =
                wechatOrderEntityMapper.selectByPrimaryKey(id);
        Long userId = wechatOrderEntity.getUserId();
        UserEntity userEntity = userEntityMapper.selectByPrimaryKey(userId);
        boolean isEntity = Optional.ofNullable(userEntity).isPresent();
        if(isEntity){
            wechatOrderEntity.setUserName(userEntity.getRealName());
            wechatOrderEntity.setUserPhone(userEntity.getMobile());
        }else{
            wechatOrderEntity.setUserPhone("暂无");
            wechatOrderEntity.setUserPhone("暂无");
        }
        return new Result(wechatOrderEntity);
    }

    /**
     * 订单编辑
     *
     * @param weChatOrderReq
     * @return
     */
    @Override
    public Result editOrder(WeChatOrderReq weChatOrderReq) {
        WechatOrderEntity wechatOrderEntity = new WechatOrderEntity();
        wechatOrderEntity.setId(weChatOrderReq.getId());
        wechatOrderEntity.setProjectName(weChatOrderReq.getProjectName());
        wechatOrderEntity.setCounts(weChatOrderReq.getCounts());
        wechatOrderEntity.setDeliveryPeriod(weChatOrderReq.getDeliveryPeriod());
        wechatOrderEntity.setContact(weChatOrderReq.getContact());
        wechatOrderEntity.setContactName(weChatOrderReq.getContactName());
        int i = wechatOrderEntityMapper.updateByPrimaryKey(wechatOrderEntity);
        return 0 == i?new Result(false):new Result(true);
    }
}
